<template>
  <el-form
    :model="admin"
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item
      label="用户名"
      prop="userName"
    >
      <el-input v-model="admin.userName"></el-input>
    </el-form-item>
    <el-form-item
      label="密码"
      prop="password"
    >
      <el-input v-model="admin.password"></el-input>
    </el-form-item>
    <el-form-item
      label="权限"
      prop="permission"
    >
      <el-radio-group v-model="admin.permission">
        <el-radio label="1">可用</el-radio>
        <el-radio label="0">禁用</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item
      label="手机号"
      prop="phone"
    >
      <el-input v-model="admin.phone"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        @click="updateOneAdmin"
      >立即修改</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import api from "../../../network/apis/api";
export default {
  data() {
    return {
      admin: {
        _id: "",
        userName: "",
        password: "",
        permission: "",
        phone: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 2,
            max: 11,
            message: "密码长度应在 2 到 11 个字符",
            trigger: "blur",
          },
        ],
        permission: [
          { required: true, message: "请选择权限", trigger: "change" },
        ],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "手机号应该为11位", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getOneAdmin();
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //获得指定的管理员
    async getOneAdmin() {
      const { data } = await api.admins.getOneAdmin(this.$route.params._id);
      this.admin = data.data;
    },
    async updateOneAdmin() {
      const { data } = await api.admins.updateOneAdmin(this.admin);
      if (data.status) {
        this.$message({
          message: "修改成功",
          type: "success",
        });
        this.$router.push("/admin/adminsList");
      }
    },
  },
};
</script>

<style>
.demo-ruleForm {
  width: 400px;
  text-align: left;
  margin-left: 200px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
